/*
* 场景管理类;
*/
class SceneManager{
    private static _instance:SceneManager;
    /**层级字典 */
    private _layerDic:Laya.Dictionary;
    private _scence:Sprite;
    /**相机 */
    private _camera2D:Camera2D;
    /**3D相机 */
    private _camera3d:Camera;
    /**3D场景 */
    private _scene3d:Scene;
    /**3D容器 */
    private _container3d:Sprite3D;
    private constructor(){

    }

    /**
     * 初始化
     */
    public init():void
    {
        this.init2D();
        this.init3D();
    }

    /**
     * 初始化2D
     */
    private init2D():void
    {
        this._layerDic = new Laya.Dictionary();
        this._scence = new Sprite();
        this._scence.width = GameConfig.deviceW;
		this._scence.height = GameConfig.deviceH;
        Laya.stage.addChild(this._scence)
        this._camera2D = new Camera2D(this._scence);
    }

    public get scene():Sprite
	{
		return this._scence;
	}

    /**
     * 初始化3D
     */
    private init3D():void{
        	this._scene3d = new Scene();
			Laya.stage.addChild(this._scene3d);
			this._camera3d = new Camera(0, 0.1, 300);
			this._scene3d.addChild(this._camera3d);
			this._camera3d.transform.translate(new Vector3(0, 0, 150));
			this._camera3d.orthographic =  true;
			this._camera3d.orthographicVerticalSize = 10;
			this._camera3d.viewport = new Laya.Viewport(0,0,GameConfig.deviceW, GameConfig.deviceH);
			
			this._container3d = new Sprite3D();
			this._container3d.transform.rotationEuler = new Vector3(Tool.A2R(30));
			this._scene3d.addChild(this._container3d);
    }

    /**
     * 3D任务层
     */
    public get container3d():Sprite3D
    {
        return this._container3d;
    }

    /**
     * 单例
     */
    public static get instance():SceneManager
    {
        if(!this._instance)
        {
            this._instance = new SceneManager();
        }
        return this._instance;
    }

    /**
     * 获取相机
     */
    public get camera2D():Camera2D
    {
        return this._camera2D;
    }
    
    public getMousePos():Laya.Point
    {
        if(this._scence)
        {
            return new Laya.Point(this._scence.mouseX,this._scence.mouseY);
        }
    }
    /**
     * 
     * @param layer 层
     * @param layerStr 层名称
     */
    public addLayer<T extends Layer>(layerClass: { new (): T; }):void
    {
        let layer:Layer =  new layerClass();
        layer.width = this._scence.width
		layer.height = this._scence.height
        this._scence.addChild(layer);
        this._layerDic.set(layer.type,layer);
    }

    /**
     * 获取层
     * @param type 层类型
     */
    public getLayer(type):void
    {
        return this._layerDic.get(type);
    }

    
    /**
     * 添加对象到层
     * @param obj 添加对象
     * @param layer 添加层级
     * @param posX 位置X
     * @param posY 位置Y
     */
    public addToLayer(obj: Sprite, layer: string, posX: number = 0, posY: number = 0): void  {
        let layeSpr: Sprite = this._layerDic.get(layer);
        if (layeSpr)  {
            obj.x = posX;
            obj.y = posY;
            layeSpr.addChild(obj);
            console.log(obj.x + "||" + obj.y)
        } else  {
            console.log("can not find layer:" + layer)
        }
    }

    /**
	* 获取所有层
     */
	public get layers(): Layer[] {
		return this._layerDic.values
	}

    public update():void
    {
        if(this._camera2D != null)
        {
            this._camera2D.update();
        }
        WorldMap.instance.updata();
    }
}